package com.probiz.estore.imports.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.RandomStringUtils;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.core.util.StringUtil;


public class ImportInfo {
	public static Short STATUS_FAIL = (short)-1; //导入失败
	public static Short STATUS_INPROCESS = (short)0; //导入中
	public static Short STATUS_SUCCESS = (short)1; //导入完成
	public static Short STATUS_USERSTOP = (short)2; //用户主动停止
	public static Short STATUS_USERSUSPEND = (short)3; //用户主动暂停
	/**
	 * 导入状态
	 */
	private Short status;
	/**
	 * 导入的文件
	 */
	private File importFile;
	/**
	 * 导入的文件的下载路径
	 */
	private String importFileUrl;
	/**
	 * 导入文件编码
	 */
	private String encoding;
	/**
	 * 导入文件的需要导入的条数
	 */
	private int importFileRowCount;
	/**
	 * 当前正在处理的行数，不算列头行
	 */
	private int currentRow=0;
	/**
	 * 成功添加数量
	 */
	private int successInsertCount=0;
	/**
	 * 失败添加数量
	 */
	private int failInsertCount=0;
	/**
	 * 成功更新数量
	 */
	private int successUpdateCount=0;
	/**
	 * 失败更新数量
	 */
	private int failUpdateCount=0;
	/**
	 * 忽略数量
	 */
	private int ignoreCount;

	/**
	 * 连续失败次数
	 */
	private int continuouslyFailCount=0;
	
	/**
	 * 开始行
	 */
	private Integer beginRow;
	
	/**
	 * 结束行
	 */
	private Integer endRow;
	/**
	 * 失败记录,(只记录第几行)
	 */
	private List<Integer> failData=new ArrayList<Integer>();
	/**
	 * 开始导入时间(本次或上次)
	 */
	private Date startImportTime=null;
	/**
	 * 结束导入时间(上次)
	 */
	private Date endImportTime=null;
	
	
	/**
	 * 失败文件
	 */
	private File failFile;
	/**
	 * 成功文件
	 */
	private File successFile;
	
	/**
	 * 失败文件下载路径
	 */
	private String failFileUrl;
	/**
	 * 成功文件下载路径
	 */
	private String successFileUrl;
	
	/**
	 * 完整日志文件
	 */
	private File logFile;
	/**
	 * 完整日志文件下载路径
	 */
	private String logFileUrl; 
	/**
	 * 失败日志文件
	 */
	private File failLogFile;
	/**
	 * 失败日志文件下载路径
	 */
	private String failLogFileUrl;
	
	
	
	private ImportInfo(){
	}
	private ImportInfo(String fileUrl,String encoding){
		synchronized(this) {
			String file = ConfigUtil.getInstance().getMediaUploadPath()+"/"+fileUrl;
			
			this.encoding = encoding;
			status = STATUS_INPROCESS;
			importFile = new File(file);
			importFileUrl  = fileUrl;
			importFileRowCount =0;
			currentRow = 0;
			successInsertCount=0;
			failInsertCount=0;
			successUpdateCount=0;
			failUpdateCount=0;
			ignoreCount=0;
			continuouslyFailCount=0;
			beginRow = 1;
			endRow = 1;
			failData=new ArrayList<Integer>();
			startImportTime=new Date();
			endImportTime=null;
			
			String fileRandomCode = generateFileRandomCode();
			String filePath = ConfigUtil.getInstance().getMediaUploadPath()+"/"+fileUrl.substring(0,fileUrl.lastIndexOf("/"));
			String fileType =  fileUrl.substring(fileUrl.lastIndexOf(".")+1);
			
			String failFileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1,fileUrl.indexOf("."))+"-"+fileRandomCode+"(fail)."+fileType; 
			failFile = new File(filePath+"/"+failFileName);
			failFileUrl = fileUrl.substring(0,fileUrl.lastIndexOf("/")+1)+failFileName;
			
			String successFileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1,fileUrl.indexOf("."))+"-"+fileRandomCode+"(success)."+fileType; 
			successFile = new File(filePath+"/"+successFileName);
			successFileUrl = fileUrl.substring(0,fileUrl.lastIndexOf("/")+1)+successFileName;
			
			String logFileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1,fileUrl.indexOf("."))+"-"+fileRandomCode+".log";  
			logFile = new File(filePath+"/"+logFileName);
			logFileUrl = fileUrl.substring(0,fileUrl.lastIndexOf("/")+1)+logFileName;
			
			String failLogFileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1,fileUrl.indexOf("."))+"-"+fileRandomCode+"(fail).log";  
			failLogFile = new File(filePath+"/"+failLogFileName);
			failLogFileUrl = fileUrl.substring(0,fileUrl.lastIndexOf("/")+1)+failLogFileName;
		}
		
	}
	/**
	 * 获得类似json结构
	 * @return
	 */
	public Map<String,Object> getJsonModel(){
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("status", status);
		model.put("importStatus", I18nUtil.getInstance().getMessage("import.status."+status.toString()+".desc"));
		model.put("importFileUrl", importFileUrl);
		model.put("importFileRowCount", importFileRowCount);
		model.put("successInsertCount", successInsertCount);
		model.put("failInsertCount", failInsertCount);
		model.put("successUpdateCount", successUpdateCount);
		model.put("failUpdateCount", failUpdateCount);
		model.put("ignoreCount", ignoreCount);
		model.put("continuouslyFailCount", continuouslyFailCount);
		model.put("beginRow", beginRow);
		model.put("endRow", endRow);
		model.put("failData", StringUtil.integerArrayToString(failData.toArray(new Integer[0]), ","));
		model.put("startImportTime", DateUtil.convertDateTimeToString(startImportTime));
		if(endImportTime!=null){
			model.put("endImportTime", DateUtil.convertDateTimeToString(endImportTime));
		}else{
			model.put("endImportTime", "");
		}
		model.put("failFileUrl", failFileUrl);
		model.put("successFileUrl", successFileUrl);
		model.put("logFileUrl", logFileUrl);
		model.put("failLogFileUrl", failLogFileUrl);
		model.put("currentRow", currentRow);
		model.put("completedPercent", getCompletedPercent());
		return model;
	}
	/**
	 * 构造函数
	 * @return
	 */
	public static ImportInfo newInstance(String file,String encoding){
		return new ImportInfo(file,encoding);
	}
	/**
	 * ---------start:自定义函数----------
	 */
	/**
	 * 获得完成的百分比
	 */
	public int getCompletedPercent(){
		if(this.getImportFileRowCount()!=0){
			return this.getCurrentRow()*100/this.getImportFileRowCount();
		}else{
			return 100;
		}
	}
	/**
	 * 产生文件随机编号
	 * @return
	 */
	private String generateFileRandomCode(){
		return DateUtil.fmtTodayToSix()+RandomStringUtils.random(8,false,true);
	}
	public void addFailCount(boolean isUpldate,Integer rowNum){
		if(isUpldate){
			failUpdateCount++;
		}else{
			failInsertCount++;
		}
		failData.add(rowNum);
		continuouslyFailCount++;
	}
	
	public void addFailInsertCount(Integer rowNum){
		failInsertCount++;
		failData.add(rowNum);
		continuouslyFailCount++;
	}
	public void addFailUpdateCount(Integer rowNum){
		failUpdateCount++;
		failData.add(rowNum);
		continuouslyFailCount++;
	}
	
	
	public void addSuccessCount(boolean isUpldate){
		if(isUpldate){
			successUpdateCount++;
		}else{
			successInsertCount++;
		}
		continuouslyFailCount=0;
	}
	public void addSuccessInsertCount(){
		successInsertCount++;
		continuouslyFailCount=0;
	}
	public void addSuccessUpdateCount(){
		successUpdateCount++;
		continuouslyFailCount=0;
	}
	
	
	public void addIgnoreCount(){
		ignoreCount++;
	}

	
	/**
	 * ---------end:  自定义函数----------
	 */
	public Short getStatus() {
		return status;
	}
	public void setStatus(Short status) {
		this.status = status;
	}
	public File getImportFile() {
		return importFile;
	}
	public void setImportFile(File importFile) {
		this.importFile = importFile;
	}
	public String getImportFileUrl() {
		return importFileUrl;
	}
	public void setImportFileUrl(String importFileUrl) {
		this.importFileUrl = importFileUrl;
	}
	public int getSuccessInsertCount() {
		return successInsertCount;
	}
	public void setSuccessInsertCount(int successInsertCount) {
		this.successInsertCount = successInsertCount;
	}
	public int getFailInsertCount() {
		return failInsertCount;
	}
	public void setFailInsertCount(int failInsertCount) {
		this.failInsertCount = failInsertCount;
	}
	public int getSuccessUpdateCount() {
		return successUpdateCount;
	}
	public void setSuccessUpdateCount(int successUpdateCount) {
		this.successUpdateCount = successUpdateCount;
	}
	public int getFailUpdateCount() {
		return failUpdateCount;
	}
	public void setFailUpdateCount(int failUpdateCount) {
		this.failUpdateCount = failUpdateCount;
	}
	public int getIgnoreCount() {
		return ignoreCount;
	}
	public void setIgnoreCount(int ignoreCount) {
		this.ignoreCount = ignoreCount;
	}
	public int getContinuouslyFailCount() {
		return continuouslyFailCount;
	}
	public void setContinuouslyFailCount(int continuouslyFailCount) {
		this.continuouslyFailCount = continuouslyFailCount;
	}
	public Integer getBeginRow() {
		return beginRow;
	}
	public void setBeginRow(Integer beginRow) {
		this.beginRow = beginRow;
	}
	public Integer getEndRow() {
		return endRow;
	}
	public void setEndRow(Integer endRow) {
		this.endRow = endRow;
	}
	public List<Integer> getFailData() {
		return failData;
	}
	public void setFailData(List<Integer> failData) {
		this.failData = failData;
	}
	public Date getStartImportTime() {
		return startImportTime;
	}
	public void setStartImportTime(Date startImportTime) {
		this.startImportTime = startImportTime;
	}
	public Date getEndImportTime() {
		return endImportTime;
	}
	public void setEndImportTime(Date endImportTime) {
		this.endImportTime = endImportTime;
	}
	public File getFailFile() {
		return failFile;
	}
	public void setFailFile(File failFile) {
		this.failFile = failFile;
	}
	public File getSuccessFile() {
		return successFile;
	}
	public void setSuccessFile(File successFile) {
		this.successFile = successFile;
	}
	public String getFailFileUrl() {
		return failFileUrl;
	}
	public void setFailFileUrl(String failFileUrl) {
		this.failFileUrl = failFileUrl;
	}
	public String getSuccessFileUrl() {
		return successFileUrl;
	}
	public void setSuccessFileUrl(String successFileUrl) {
		this.successFileUrl = successFileUrl;
	}
	public File getLogFile() {
		return logFile;
	}
	public void setLogFile(File logFile) {
		this.logFile = logFile;
	}
	public String getLogFileUrl() {
		return logFileUrl;
	}
	public void setLogFileUrl(String logFileUrl) {
		this.logFileUrl = logFileUrl;
	}
	public int getImportFileRowCount() {
		return importFileRowCount;
	}
	public void setImportFileRowCount(int importFileRowCount) {
		this.importFileRowCount = importFileRowCount;
	}
	public int getCurrentRow() {
		return currentRow;
	}
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
	public String getEncoding() {
		return encoding;
	}
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	public File getFailLogFile() {
		return failLogFile;
	}
	public void setFailLogFile(File failLogFile) {
		this.failLogFile = failLogFile;
	}
	public String getFailLogFileUrl() {
		return failLogFileUrl;
	}
	public void setFailLogFileUrl(String failLogFileUrl) {
		this.failLogFileUrl = failLogFileUrl;
	}
	
}
